//题型管理控制脚本。
Ext.define("ES.controller.ItemTypesController",{
    extend:"Ext.app.Controller",
    models:["ItemTypesModel"],
    stores:["ItemTypesStore"],
    views:["itemtypes.ItemTypesList","itemtypes.ItemTypesEdit"],
    init:function(){
        this.control({
            "ItemTypesTabList":{
                itemdblclick:this.onEdit
            },
            "ItemTypesTabList button[action=btnQuery]":{
                click:this.onSearch
            },
            "ItemTypesTabList button[action=btnAdd]":{
                click:this.onAdd
            },
            "ItemTypesTabList button[action=btnDelete]":{
                click:this.onDelete
            },
            "itemTypesEdit button[action=btnSave]":{
                click:this.onSave
            },
            "itemTypesEdit button[action=btnCancel]":{
                click:function(btn){
                    var win = btn.up("itemTypesEdit");
                     if(win){
                         win.close();
                     }
                }
            }
        });
    },
    onSearch:function(btn){
        var toolbar = btn.ownerCt;
        if(toolbar){
            var grid = toolbar.ownerCt;
            if(grid){
                var st = grid.getStore();
                if(st){
                    st.load({
                        typeCode:toolbar.getComponent(1).getValue(),
                        typeName:toolbar.getComponent(3).getValue()
                    })
                }
            }
        }
    },
    onAdd:function(){
        var win = Ext.widget("itemTypesEdit");
        if(win){
            win.show();
        }
    },
    onEdit:function(view,record,item,index,e,opt){
        var win = Ext.widget("itemTypesEdit");
        if(win){
            var frm = win.down("form");
            if(frm){
                frm.loadRecord(record);
                win.show();
            }
        }
    },
    onSave:function(btn){
        var win =btn.up("itemTypesEdit");
        if(win){
            var form = btn.up("form").getForm();
            if(form && form.isValid()){
                var store = this.getItemTypesStoreStore();
                var record = form.getRecord();
                var values = form.getValues();
                if(record){
                    record.set(values);
                }else{
                    record = this.getItemTypesModelModel().create();
                    record.set(values);
                    store.insert(0,record);
                }
                store.sync();
            }
            win.close();
        }
    },
    onDelete:function(btn){
        Ext.MessageBox.confirm("删除提示","确认要删除选中的数据吗？", function(cfm){
            if(cfm == "yes"){
                var grid = btn.ownerCt.ownerCt;
                var data = grid.getSelectionModel().getSelection();
                if(data.length == 0){
                    Ext.Msg.alert("提示","您最少要选择一条数据!");
                }else{
                    var st = grid.getStore();
                    Ext.Array.each(data, function(record){
                       st.remove(record); 
                    });
                    st.sync();
                }
            }
        });
    }
})